<!--
    Copyright 2010 Justin Lipton, Jonathan Rosenberg
    
    This file is part of MediaList.

    MediaList is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaList is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaList.  If not, see <http://www.gnu.org/licenses/>.
-->

<?php
$user=$_GET["user"];
$showid=$_GET["showid"];

$con = mysql_connect("localhost", "root");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("DB", $con);

$sql="SELECT * FROM ".$user." WHERE ShowId='".$showid."'";
$result = mysql_query($sql, $con);
$row = mysql_fetch_array($result);

$sql="SELECT * FROM Users WHERE Username='".$user."'";
$result=mysql_query($sql, $con);
$row4=mysql_fetch_array($result);

if($row["Status"]=="W")
{
    $sql="UPDATE Users SET NumViewing='".($row4["NumViewing"]-1)."' 
    WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
else if($row["Status"]=="C")
{
    $sql="UPDATE Users SET NumCompleted='".($row4["NumCompleted"]-1)."'
    WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
else if($row["Status"]=="D")
{
    $sql="UPDATE Users SET NumDropped='".($row4["NumDropped"]-1)."'
    WHERE Username='".$user."'";
    mysql_query($sql, $con);
}
else if($row["Status"]=="P")
{
    $sql="UPDATE Users SET NumPlanning='".($row4["NumPlanning"]-1)."'
    WHERE Username='".$user."'";
    mysql_query($sql, $con);
}

$sql="SELECT * FROM Shows WHERE Showname='".$row["ShowName"]."'";
$result=mysql_query($sql, $con);
$row2=mysql_fetch_array($result);

$sql = "UPDATE Shows SET TotalScores='".($row2["TotalScores"]-$row["Rating"])."', 
TotalSeen='".($row2["TotalSeen"]-1)."' WHERE Showname='".$row["ShowName"]."'";
mysql_query($sql, $con);

$sql="DELETE FROM ".$user." WHERE ShowId='".$showid."'";
mysql_query($sql);

$sql="DELETE FROM UserShowJoin WHERE Username='".$user."' AND Showname='".$row["ShowName"]."'";
mysql_query($sql);

mysql_close($con);
?>